/**
 * Project  : Looker
 * Package  : net.freestu.looker.impl
 * File     : LookerMysqlNewsListener.java
 * Author   : solosky < solosky772@qq.com >
 * Date     : 2009-9-3
 * Modified : 2009-9-3
 * License  : Apache License 2.0 
 */
package net.freestu.looker.impl;


import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;

import net.freestu.looker.common.ListenerException;
import net.freestu.looker.common.Media;
import net.freestu.looker.common.NewsEntity;
import net.freestu.looker.common.NewsListener;
import net.freestu.looker.common.NewsMeta;
import net.freestu.looker.common.SiteManager;
import net.freestu.looker.util.HtmlUtils;
import net.freestu.looker.util.MysqlQuery;
import net.freestu.looker.util.NewsUtils;
import net.freestu.looker.util.Starter;

import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;


/**
 *  把新闻数据保存到Mysql数据库
 */
public class MysqlNewsListener implements NewsListener
{

	/* (non-Javadoc)
     * @see net.freestu.looker.common.NewsListener#add(net.freestu.looker.common.NewsEntity)
     */
    @Override
    public void add(NewsEntity newsEntity) throws ListenerException
    {
    	//添加新闻数据值数据库
		StringBuffer sql = new StringBuffer();
		String original = NewsUtils.getOriginal(newsEntity);
		sql.append("INSERT INTO looker_news ( ");
		sql.append("`newshash` ,`title`, `author` ,`date` ,`intro`, `brief`, `original`,");
		sql.append("`categoryhash` ,`tag` ,`url` ,`media`, `thumb`,`image`, `status`");
		sql.append(") VALUES (");
		sql.append(newsEntity.getHash());
		sql.append(", '");
		sql.append(HtmlUtils.sqlSafe(newsEntity.getTitle()));
		sql.append("', '");
		sql.append(newsEntity.getAuthor());
		sql.append("', ");
		sql.append(newsEntity.getDate().getTime()/1000);
		sql.append(", '");
		sql.append(HtmlUtils.sqlSafe(NewsUtils.getIntro(newsEntity)));
		sql.append("', '");
		sql.append(HtmlUtils.sqlSafe(NewsUtils.getBrief(newsEntity, original)));
		sql.append("', '");
		sql.append(HtmlUtils.sqlSafe(original));
		sql.append("', ");
		sql.append(newsEntity.getCategory().getHash());
		sql.append(", '");
		sql.append(newsEntity.getTag());
		sql.append("', '");
		sql.append(newsEntity.getUrl());
		sql.append("',");
		sql.append(newsEntity.getMediaList().size());
		sql.append(",'");
		sql.append(NewsUtils.getThumb(newsEntity));
		sql.append("', "+NewsUtils.getImageCount(newsEntity));
		sql.append(", 1);");
		
		try {
	        MysqlQuery.getInstance().execute(sql.toString());
        } catch (SQLException e) {
	       throw new ListenerException(e.getMessage());
        }
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.common.NewsListener#close()
     */
    @Override
    public void close() throws ListenerException
    {
	    // TODO Auto-generated method stub
	    
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.common.NewsListener#commit()
     */
    @Override
    public void commit() throws ListenerException
    {
	    // TODO Auto-generated method stub
	    
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.common.NewsListener#delete(net.freestu.looker.common.NewsMeta)
     */
    @Override
    public void delete(NewsMeta news) throws ListenerException
    {
	    // TODO Auto-generated method stub
	    
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.common.NewsListener#init(org.apache.commons.configuration.Configuration)
     */
    @Override
    public void init(Configuration conf) throws ListenerException
    {
	    // TODO Auto-generated method stub
	    
    }

	/* (non-Javadoc)
     * @see net.freestu.looker.common.NewsListener#update(net.freestu.looker.common.NewsEntity)
     */
    @Override
    public void update(NewsEntity news) throws ListenerException
    {
    	System.out.println("newsupdated:"+news.getTitle());
    }
    
    
    public static void main(String[] args) throws ConfigurationException, ListenerException
    {
    	Starter s = new Starter();
    	s.loadConfiguration("./conf/looker.xml");
    	NewsEntity n = new NewsEntity();
    	n.setUrl("http://localhost/doc/1a.php");
    	n.setCategory(SiteManager.getInstance().getSiteList().get(0).getCategoryList().get(0));
    	String html ="正文";
    	n.setContent(html);
    	n.setHtml(html);
    	n.setMediaList(new ArrayList<Media>());
    	n.setTitle("这是标题");
    	n.setAuthor("作者");
    	n.setDate(new Date());
    	new MysqlNewsListener().add(n);
    }
}
